package com.chzc.fm.duotin.ui.home;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.ui.home.utils.HomePalyListDataList;
import com.chzc.fm.duotin.ui.home.utils.HomePlayListData;
import com.chzc.fm.duotin.ui.home.utils.HomeUtils;
import com.chzc.fm.duotin.utils.DuotinTask;
import com.chzc.fm.duotin.utils.LogUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Administrator on 2016/6/30.
 */
public class Play1PlayListActivity extends AppCompatActivity {

    private ImageView ivBack,ivSort;

    private TextView tvTitle;

    private ListView lvPlayList;
    private List<HomePalyListDataList> dataList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_play1_palylist);
            initViews();
            initEvents();
            initData();
    }

    private void initData() {

        final PlayListAdapter adapter = new PlayListAdapter(this,dataList);
        lvPlayList.setAdapter(adapter);
        //面板id
        String id = getIntent().getStringExtra("urlID");

        HomeUtils.getPlay1PlayList(new DuotinTask.IRequestCallback() {
            @Override
            public void success(Object object) {

                try {
                    JSONObject root = new JSONObject(object.toString());

                    String message = root.getString("error_msg");

                    if ("success".equals(message)) {


                        JSONObject data = root.getJSONObject("data");
                        HomePlayListData dataBean = HomePlayListData.objectFromData(data.toString());
                        LogUtil.e("dataList--"+data.toString());
                        dataList = dataBean.getData_list();
                        LogUtil.e("dataList--"+dataList.toString());
                        adapter.setmList(dataList);
                        adapter.notifyDataSetChanged();

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void error(String msg) {

            }
        },id);


    }

    private void initEvents() {

        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void initViews() {
        ivBack = (ImageView) findViewById(R.id.iv_back);
        ivSort = (ImageView) findViewById(R.id.iv_sort);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        lvPlayList = (ListView) findViewById(R.id.lv_playlist);

    }

    //创建一个Adapter类

    private class PlayListAdapter extends BaseAdapter{

        private LayoutInflater inflater;

        public List<HomePalyListDataList> getmList() {
            return mList;
        }

        public void setmList(List<HomePalyListDataList> mList) {
            this.mList = mList;
        }

        private List<HomePalyListDataList > mList;

        public PlayListAdapter(Context context, List<HomePalyListDataList> list) {

            inflater = LayoutInflater.from(context);
            mList = list;
        }

        @Override
        public int getCount() {
            int i = mList==null?0:mList.size();

            Log.e("mList.size"," - "+i);
            return i;
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView==null){
                holder = new ViewHolder();
                convertView= inflater.inflate(R.layout.activity_play1_playlist_listview,null);
                holder.tvContent = (TextView) convertView.findViewById(R.id.tv_content);
                holder.tvCountLeft = (TextView) convertView.findViewById(R.id.tv_countleft);
                holder.tvTimeRight = (TextView) convertView.findViewById(R.id.tv_countright);
                holder.ivDown = (ImageView) convertView.findViewById(R.id.iv_down);

                convertView.setTag(holder);
            }else{

                holder = (ViewHolder) convertView.getTag();
                holder.tvCountLeft.setText(mList.get(position).getPlay_num());
                holder.tvTimeRight.setText(mList.get(position).getDuration());
            }
            //设置布局中要显示的视图


            return convertView;
        }

        private class ViewHolder {

            public TextView tvContent,tvCountLeft,tvTimeRight;
            public ImageView ivDown;

        }
    }
}
